/* eslint no-unused-vars:0 */
import React from 'react';
import PubSub from 'pubsub-js';
import './../index.less';

import { Link } from 'react-router-dom';
import { Row, Col } from 'antd';
import { Table, Button, Divider, Popconfirm, Transfer } from 'antd';

const url = 'userAndSentiment';
export default class SentimentUser extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        let temp = JSON.parse(sessionStorage.getItem('authority'));

        this.setState({
            publicSentiment: temp
        });

        this.getData(temp.id);
    }

    state = {
        mockData: [],
        targetKeys: [],
        publicSentiment: {
            id: ''
        }
    };

    // 获取当前报告板块的用户授权情况
    getData = (publicSentimentId) => {
        this.$post(url + '/getSentimentUser', publicSentimentId)
            .then((res) => {
                const mockData = [];
                const targetKeys = [];

                var dataList = res.result;
                for (var i = 0; i < dataList.length; i++) {
                    const data = dataList[i].user;

                    data['key'] = data.id;

                    if (dataList[i].selected) {
                        targetKeys.push(data.key);
                    }
                    mockData.push(data);
                }

                this.setState({ mockData, targetKeys });
            })
            .catch((error) => {
                console.log(error);
            });
    };

    // 修改报告板块授权
    updateData = () => {
        this.$post(url + '/setSentimentUserByList', {
            publicSentimentId: this.state.publicSentiment.id,
            userIdList: this.state.targetKeys
        })
            .then((res) => {
                if (res) {
                    const mockData = [];
                    const targetKeys = [];

                    var dataList = res.result;
                    for (var i = 0; i < dataList.length; i++) {
                        const data = dataList[i].user;

                        data['key'] = data.id;

                        if (dataList[i].selected) {
                            targetKeys.push(data.key);
                        }
                        mockData.push(data);
                    }

                    this.setState({ mockData, targetKeys });

                    this.$showNotification('success', '修改授权成功');
                } else this.$showNotification('error', '修改授权失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    handleChange = (targetKeys) => {
        this.setState({ targetKeys });
        console.log(targetKeys);
    };

    renderFooter = () => (
        <Button
            size="small"
            style={{ float: 'right', margin: 5 }}
            onClick={this.getMock}>
            reload
        </Button>
    );

    goback() {
        this.props.history.goBack();
    }

    render() {
        return (
            <div className="body">
                <span className="opinion-body">舆情报告管理</span>
                <Row>
                    <Button onClick={this.goback.bind(this)}>返回上一层</Button>
                </Row>
                <Row className="overview-row2" style={{ padding: '20px' }}>
                    <center>
                        <div>
                            <Transfer
                                dataSource={this.state.mockData}
                                showSearch
                                listStyle={{
                                    width: 250,
                                    height: 300
                                }}
                                operations={['to right', 'to left']}
                                targetKeys={this.state.targetKeys}
                                onChange={this.handleChange}
                                render={(item) =>
                                    `${item.collegeId}-${item.username}`
                                }
                            />
                        </div>
                    </center>
                    <center>
                        <Button onClick={this.updateData}>授权</Button>
                    </center>
                </Row>
            </div>
        );
    }
}
